﻿using AllanXingCommonLibrary;
using cnBlogLibrary;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


namespace BackgroundAgant
{
  internal  class MainPageViewModel_lightWeight : GetFeedsBase
    {
        private List<ArticleInfo> _articleInfoList;

        public List<ArticleInfo> ArticleInfoList
        {
            get { return this._articleInfoList; }
            private set { this.SetProperty(ref this._articleInfoList, value); }
        }

        internal MainPageViewModel_lightWeight(string categoryUrl = null)
        {
            ArticleInfoList = new List<ArticleInfo>();
        }

        protected override void Parse(string feed)
        {
            MatchCollection matches = Regex.Matches(feed, "<div class=\"post_item\">.*?<div class=\"digg\">.*?<div class=\"post_item_body\".*?></div>");
            foreach (Match matche in matches)
            {
                ArticleInfo artinfo = new ArticleInfo();
                string regex_article = matche.Value;
                GroupCollection gp = Regex.Match(regex_article, "<h3><a class=\"titlelnk\" href=\"(?<link>.*?)\".*?>(?<title>.*?)</a></h3>").Groups;
                artinfo.Title = gp["title"].Value;
                artinfo.Link = gp["link"].Value;
                artinfo.CommentCount = int.Parse(Regex.Match(regex_article, "<span class=\"diggnum\" id=\".*?\">(?<CommentCount>.*?)</span>").Groups["CommentCount"].Value);
                GroupCollection gp1 = Regex.Match(regex_article, "<p class=\"post_item_summary\">(?<Overview>.*?)</p>").Groups;
                artinfo.Overview = gp1["Overview"].Value.Replace("\r", string.Empty).Trim();
                GroupCollection gpwriteraAndtime = Regex.Match(regex_article, "<a href=\".*?\" class=\"lightblue\">(?<writer>.*?)</a> \r (?<PublishTime>.*?)\r *<span class=\"article_comment\">").Groups;
                artinfo.PublishTime = gpwriteraAndtime["PublishTime"].Value.TrimStart();
                artinfo.Writer = gpwriteraAndtime["writer"].Value;
                Match m_hl = Regex.Match(artinfo.Overview, "<a href=\"(?<writerhomelink>.*?)\" target=\"_blank\">");
                if (m_hl.Length > 0)
                {
                    artinfo.Overview = artinfo.Overview.Replace(m_hl.Value, string.Empty).Trim();
                    artinfo.WriterHomeLink = m_hl.Groups["writerhomelink"].Value;
                    Match mil = Regex.Match(artinfo.Overview, "<img .*? src=\"(?<WriterAvatar>.*?)\" .*?/></a>");
                    artinfo.Overview = artinfo.Overview.Replace(mil.Value, string.Empty);
                    artinfo.WriterAvatar = mil.Groups["WriterAvatar"].Value;

                }
                ArticleInfoList.Add(artinfo);
            }
        }

        protected override string GetUrl()
        {
            string url = AppResource.cnBlogsUlr;
            url += ("p" + 1);
            return url;
        }
    }
}
